﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Data;
using WebApplication1.Models.ViewModels;

namespace WebApplication1.AppServices
{
    public class ArtMuseumController : ApiController
    {
        public IHttpActionResult Get()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<UserViewModel> users = db.Users.Select(a => new UserViewModel
                {
                    UserId = a.Id,
                    Username = a.UserName
                }).ToList();
                
                return Ok(users);
            }
        }

        public IHttpActionResult Get(string id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                UserDetailViewModel user = new UserDetailViewModel()
                {
                    // Not the best practice...needs refactoring D:
                    UserId = db.Users.Where(a => a.Id == id).Select(a => a.Id).SingleOrDefault(),
                    Username = db.Users.Where(b => b.Id == id).Select(b => b.UserName).SingleOrDefault(),
                    ArtWorks = db.Artworks.Where(c => c.UserId == id).Select(c => new ArtWorkViewModel
                    {
                        ArtWorkId = c.ArtWorkId,
                        Description = c.Description,
                        Title = c.Title,
                        UserId = c.UserId
                    }).ToList()
                };
                return Ok(user);
            }
        }
    }
}
